package com.dream.keigezhushou.Activity.acty.play;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.keigezhushou.Activity.Uiutils.JsonParse;
import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.adapter.EvaluateAdapter;
import com.dream.keigezhushou.Activity.bean.VideoDetailsBean;
import com.dream.keigezhushou.Activity.pop.ReportPopupWindow;
import com.dream.keigezhushou.Activity.url.NetURL;
import com.dream.keigezhushou.Activity.view.CircleImageView;
import com.dream.keigezhushou.R;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class VideoInfoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.video_player)
    JCVideoPlayerStandard videoPlayer;
    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.iv_set)
    ImageView ivSet;
    @BindView(R.id.lv_evaluate)
    ListView lvEvaluate;
    @BindView(R.id.rl_write_evaluate)
    RelativeLayout rlWriteEvaluate;
    @BindView(R.id.activity_video_info)
    FrameLayout activityVideoInfo;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.ll_videoinfo)
    LinearLayout llVideoinfo;
    @BindView(R.id.ll_no_net)
    LinearLayout llNoNet;
    private Intent intent;
    private View mHeadView;
    private CircleImageView ivUserPic;
    private TextView tvUserNick;
    private TextView tvReport;
    private TextView tvZan;
    private TextView tvDate;
    private TextView tvDescribe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_info);
        ButterKnife.bind(this);

        initView();
        initData();
    }


    //初始化界面
    protected void initView() {

        //请求网络，更新数据
        OkHttpUtils.post()
                .url(NetURL.VideoDetails)
                .addParams("video_id", "1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                        //网络请求失败
                        llVideoinfo.setVisibility(View.GONE);
                        rlWriteEvaluate.setVisibility(View.GONE);
                        llNoNet.setVisibility(View.VISIBLE);
                        tvTitle.setVisibility(View.VISIBLE);
                        rlTitle.setBackgroundResource(R.color.colorRefund);

                        llNoNet.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                initView();
                            }
                        });

                    }

                    @Override
                    public void onResponse(String response) {

                        //网络请求成功
                        llVideoinfo.setVisibility(View.VISIBLE);
                        rlWriteEvaluate.setVisibility(View.VISIBLE);
                        llNoNet.setVisibility(View.GONE);
                        tvTitle.setVisibility(View.GONE);
                        rlTitle.setBackgroundResource(R.color.transparent);

                        VideoDetailsBean detailsBean = JsonParse.getFromJson(response,VideoDetailsBean.class);

                        //设置视频连接地址
                        videoPlayer.setUp(detailsBean.getVideo().getUrl(), JCVideoPlayer.SCREEN_LAYOUT_LIST);

                        //ListView的头部局
                        mHeadView = View.inflate(VideoInfoActivity.this, R.layout.layout_videoinfo_headview, null);
                        lvEvaluate.addHeaderView(mHeadView);

                        ivUserPic = (CircleImageView) mHeadView.findViewById(R.id.iv_user_pic);
                        tvUserNick = (TextView) mHeadView.findViewById(R.id.tv_user_nick);
                        tvReport = (TextView) mHeadView.findViewById(R.id.tv_report);
                        tvZan = (TextView) mHeadView.findViewById(R.id.tv_zan);
                        tvDate = (TextView) mHeadView.findViewById(R.id.tv_date);
                        tvDescribe = (TextView) mHeadView.findViewById(R.id.tv_describe);

                        tvUserNick.setText(detailsBean.getVideo().getNickname());
                        tvDescribe.setText(detailsBean.getVideo().getContent());
                        tvZan.setText(detailsBean.getVideo().getFabnums());

                        //点赞点击事件
                        tvZan.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                Toast.makeText(VideoInfoActivity.this, "点赞成功", Toast.LENGTH_SHORT).show();
                            }
                        });

                        //举报点击事件
                        tvReport.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                final ReportPopupWindow window = new ReportPopupWindow(View.inflate(VideoInfoActivity.this, R.layout.layout_report_reason, null), VideoInfoActivity.this, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

                                window.showPopAtLocation(activityVideoInfo, Gravity.BOTTOM);
                                window.setItemListener(new ReportPopupWindow.ItemClickListener() {
                                    @Override
                                    public void onClick() {

                                    }

                                    @Override
                                    public void onSingDetail() {

                                        //网络请求
                                        LayoutInflater inflater = getLayoutInflater();

                                        View toastView = inflater.inflate(R.layout.layout_set_toast, null);
                                        TextView tvToast = (TextView) toastView.findViewById(R.id.tv_toast);
                                        tvToast.setText("举报成功");
                                        Toast toast = new Toast(VideoInfoActivity.this);
                                        toast.setGravity(Gravity.CENTER, 0, 25);
                                        toast.setDuration(Toast.LENGTH_SHORT);
                                        toast.setView(toastView);
                                        toast.show();

//                        window.backGroundAlpha(0.4f);
                                    }
                                });

                            }
                        });

                        List<VideoDetailsBean.CommentsBean> commentsList = detailsBean.getComments();

                        //ListView创建适配器
                        lvEvaluate.setDivider(null);
                        lvEvaluate.setAdapter(new EvaluateAdapter(VideoInfoActivity.this,detailsBean,commentsList));


                    }
                });

    }


    //初始化数据
    protected void initData() {

        ivReturn.setOnClickListener(this);
        ivSet.setOnClickListener(this);
        rlWriteEvaluate.setOnClickListener(this);
    }


    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.iv_return:

                //返回
                finish();
                break;

            case R.id.iv_set:
                break;

            case R.id.rl_write_evaluate:

                intent = new Intent(this, PublishEvaluateActivity.class);
                startActivity(intent);
                break;

        }
    }
}
